<?php

namespace app\api\controller\index;

use app\api\model\common\ArticlesModel;
use app\api\model\common\CoinModel;
use app\api\model\common\OrePoolModel;
use app\api\request\index\HomeRequest;
use app\api\validate\user\UserRequest;
use app\common\controller\Api;
use think\Loader;

class Home extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * Notes: 首页公告信息
     * User: 浪子膏
     * Date: 2023/10/21 14:17
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function notice()
    {
        $list = ArticlesModel::where('category_id', 14)->limit(5)
            ->order('weigh desc')->field('id,weigh,image,name')
            ->select();

        $this->dataOk($list);
    }

    /**
     * Notes: 公告分页列表
     * User: 浪子膏
     * Date: 2023/10/21 14:20
     * @throws \think\exception\DbException
     */
    public function noticeList()
    {
        $params = input('post.');
        $list = ArticlesModel::where('category_id', 14)
            ->order('weigh desc')->field('id,weigh,image,name,create_time')
            ->paginate($params['page_size'] ?? 15)->each(function ($item) {
                $item['create_time'] = date('Y/m/d', $item['create_time']);
            });

        $this->dataOk($list);
    }

    /**
     * Notes: 帮助中心分页列表
     * User: 浪子膏
     * Date: 2023/10/21 14:20
     * @throws \think\exception\DbException
     */
    public function helpList()
    {
        $params = input('post.');
        $list = ArticlesModel::where('category_id', 15)
            ->order('weigh desc')->field('id,weigh,image,name,create_time')
            ->paginate($params['page_size'] ?? 15)->each(function ($item) {
                $item['create_time'] = date('Y/m/d', $item['create_time']);
            });

        $this->dataOk($list);
    }

    /**
     * Notes: 文章详情
     * User: 浪子膏
     * Date: 2023/10/21 14:25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function articlesInfo()
    {
        $params = request()->post();

        $validate = Loader::validate(HomeRequest::class);
        if (!$validate->scene('noticeInfo')->check($params)) $this->error($validate->getError());

        $articlesModel = new ArticlesModel();
        $info = $articlesModel->where('id', $params['id'])->find();
        if (empty($info)) {
            $this->error(__('That information has been lost'));
        }

        $this->dataOk($info);

    }

    /**
     * Notes:获取币种列表
     * User: 浪子膏
     * Date: 2023/10/22 14:33
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function coinList()
    {
        $coinModel = new CoinModel();
        $list = $coinModel->order('id')->select();
        foreach ($list as &$item) {
            $item['mine_ore_pool'] = OrePoolModel::where('id', 1)
                ->withField('create_time,update_time,deletetime')->find();

            $cycle_end = strtotime($item['cycle_end']);
            if ($cycle_end < time()) {
                $item['cycle_end_day'] = 0;
            } else {
                $item['cycle_end_day'] = round(($cycle_end - time()) / 86400);
            }

            $item['minimum_payment'] = floatval($item['minimum_payment']);
            switch ($item['scale']) {
                case 'P':
                    $s = 1000000000000000;
                    break;
                case 'T':
                    $s = 1000000000000;
                    break;
                case 'G':
                    $s = 1000000000;
                    break;
                case 'M':
                    $s = 1000000;
                    break;
                case 'B':
                    $s = 1000;
                    break;
                default:
                    $s = 1;
            }
            $item['network_diff'] =round($item['network_diff']/$s,2).'/'.$item['scale'];
        }
        $this->dataOk($list);
    }


    /**
     * Notes:获取币种详情
     * User: 浪子膏
     * Date: 2023/10/22 14:45
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function coinInfo()
    {
        $params = request()->post();
        $validate = Loader::validate(HomeRequest::class);
        if (!$validate->scene('coinInfo')->check($params)) $this->error($validate->getError());

        $coinModel = new CoinModel();
        $info = $coinModel->where('id', $params['id'])->find();
        if (empty($info)) {
            $this->error(__('That information has been lost'));
        }

        $this->dataOk($info);
    }

    /**
     * Notes:收益计算器
     * User: 浪子膏
     * Date: 2023/10/22 15:10
     */
    public function calculate()
    {
        $params = request()->post();
        $validate = Loader::validate(HomeRequest::class);
        if (!$validate->scene('calculate')->check($params)) $this->error($validate->getError());

        $data['day_income'] = round($params['per_unit'] / config('site.usd') * $params['net_hashrate'], 8);
        $data['day_usdt'] = round($data['day_income'] * $params['coin_price_usd'], 2);

        $data['month_income'] = round($data['day_income'] * 30, 8);
        $data['month_usdt'] = round($data['day_usdt'] * 30, 8);

        $data['year_income'] = round($data['month_income'] * 365, 8);
        $data['year_usdt'] = round($data['month_usdt'] * 365, 8);

        $this->dataOk($data);
    }
}